Energy Solutions Germany
 
Deep learning workshop
 
In [94]:
print("hello")
hello

Welcome to the interactive ML workshop!

 
We are using a jupyter notebook, which is a standard interactive, collaborative data science toolkit
 
The name refers to Julia, Python, and R, which are popular data science languages
 
In this workshop we will use the python language, which was developed by Dutchie Guido van Rossum.
 
It is the most popular, and fastest growing data science language.
So why do we need machine learning?
 

Increasing depth in data

  • Dummy = Yes or No, 0 or 1, True or False
  • Scalar = A number, for example 255 kWh
  • Vector = A time series of kWh
  • Matrix = A vector of kWh by location
  • Tensor = A matrix multiplied by at least on matrix (Multi-dimensional array), for example ESG's Loctions list as XY coordinates attachted to the time-series matrix

Key takeaways

  1. Up to date about imbalance progress
  2. Understand AI key concepts - transfer learning, model architecture, data requirements
  3. Able to do some AI yourself

In this workshop we will do "computer vision", one particular area of machine learning

This is good to work with interactively because you can see with your own eyes what is going on in the neural network...

... all concepts apply to other areas of machine learning, and artificial intelligence more broadly

From vision, to speech, text, and numbers

In [1]:
#Let's start by setting up the environment that we will use during this workshop

import os, requests, json, os.path
from fastai.vision import *
from fastai import *
from fastai.callbacks.hooks import *
#from google_images_download import google_images_download
#response = google_images_download.googleimagesdownload()
from PIL import Image as pil_image
PIL.Image.LOADTRUNCATEDIMAGES = True

Ok, so what do we want to classify on?

In [95]:
os.chdir('C:/images/')
os.makedirs('classes', exist_ok=True)
path = 'C:/images/classes/'
class_list = [(f,f.replace('.csv','')) for f in os.listdir(path) if '.csv' in f]
for _,c in class_list:
    loc = os.path.join(path,c)
    if not os.path.isdir(loc):
        os.mkdir(loc)
print(class_list)
[('chp mtu.csv', 'chp mtu'), ('chp mwn.csv', 'chp mwn'), ('jenbacher_chp.csv', 'jenbacher_chp')]
In [96]:
for f,c in class_list:
    print(f,c)
    csv = os.path.join(path,f)
    print(csv)
    download_images(csv,os.path.join(path,c))
    verify_images(os.path.join(path,c), delete=True, max_size=500)
In [97]:
# do we have enough data?

DIR = 'C:/images/classes/'
d ={name:len(os.listdir(os.path.join(DIR,name))) for name in os.listdir(DIR) if not os.path.isfile(os.path.join(DIR, name))}
for k,v in d.items():
    print(k,':',v)
    print('')
chp mtu : 62

chp mwn : 88

jenbacher_chp : 91

In [98]:
# prepping the data...

np.random.seed(42)
data = ImageDataBunch.from_folder("C:/images/", train=".", valid_pct=0.2,
        ds_tfms=get_transforms(), size=224, num_workers=4).normalize(imagenet_stats)
In [99]:
# is everything ok?

data.show_batch(rows=3, figsize=(7,8))
In [100]:
len(data.train_ds), len(data.valid_ds)
Out[100]:
(190, 47)
In [107]:
# so here we choose the model architecture!

learn = cnn_learner(data, models.resnet34, metrics=[error_rate, accuracy])
In [ ]:
learn.load('pre_trained_chp')
In [109]:
learn.fit_one_cycle(2)
epoch train_loss valid_loss error_rate accuracy time
0 0.633854 1.757635 0.340426 0.659574 01:48
1 0.451185 1.498469 0.297872 0.702128 01:50
In [110]:
learn.validate()
Out[110]:
[1.4984691, tensor(0.2979), tensor(0.7021)]
While we're waiting - what's actually going on in this neural network?
 
In [111]:
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()
In [112]:
interp.plot_top_losses(9)
In [105]:
# so what would happen with random weights, instead of starting with a pre-trained network (transfer learning)?

learn.save('pre_trained_chp')
del learn
In [81]:
learn_wo = cnn_learner(data, models.resnet34, metrics=[error_rate,accuracy], pretrained=False)
c:\program files\python37\lib\site-packages\fastai\vision\learner.py:106: UserWarning: `create_cnn` is deprecated and is now named `cnn_learner`.
  warn("`create_cnn` is deprecated and is now named `cnn_learner`.")
In [82]:
learn_wo.fit_one_cycle(4)
epoch train_loss valid_loss error_rate accuracy time
0 2.383491 6.885210 0.720000 0.280000 05:09
1 1.988807 1.177455 0.480000 0.520000 05:05
2 1.761498 1.375785 0.520000 0.480000 05:05
3 1.619428 1.087470 0.430000 0.570000 05:05
What is fitting and why does it matter?
 
In [83]:
interp_wo = ClassificationInterpretation.from_learner(learn_wo)
In [84]:
# not so good in comparison

interp.plot_confusion_matrix()
plt.figure()
interp_wo.plot_confusion_matrix()
<Figure size 432x288 with 0 Axes>
In [85]:
interp_wo.plot_top_losses(9)
In [86]:
learn_wo.save('trained')
In [ ]:
learn.load('pre_trained')
In [87]:
m = learn_wo.model.eval();
In [88]:
idx=18
x,y = data.valid_ds[idx]
x.show()
x,_ = data.one_item(x)
In [89]:
x_im = Image(data.denorm(x)[0])
#Image(data.denorm(x)[0])
In [90]:
def hooked_backward(cat=y):
    with hook_output(m[0]) as hook_a: 
        with hook_output(m[0], grad=True) as hook_g:
            preds = m(x)
            preds[0,int(cat)].backward()
    return hook_a,hook_g

hook_a,hook_g = hooked_backward()
In [91]:
acts  = hook_a.stored[0].cpu()
acts.shape
avg_acts = acts.mean(0)
avg_acts.shape
Out[91]:
torch.Size([7, 7])
In [92]:
def show_heatmap(hm):
    _,ax = plt.subplots()
    x_im.show(ax)
    ax.imshow(hm, alpha=0.6, extent=(0,224,224,0),
              interpolation='bilinear', cmap='magma');
In [93]:
show_heatmap(avg_acts)

schalke%2004%20logo%20heatmap.png

Takeaway concepts

  • Model architecture
  • Transfer learning
  • Freezing
  • Parameters/weights/coefficients
  • Over/underfitting Fitting
  • Stochastic Gradient descent
  • Loss function
  • Training batches
  • Learning rate
Now you! We will supervise
In [ ]: